package com.ujs.servicevod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.google.gson.Gson;
import com.ujs.servicebase.exception.GuliException;
import com.ujs.servicevod.config.AlyVodClient;
import com.ujs.servicevod.service.VodService;
import com.ujs.servicevod.utils.ConstantPropertiesUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author zhangshihao
 * @create 2023-08-09 12:30
 */
@Service
public class VodServiceImpl implements VodService {

    @Override
    public String uploadVideoAly(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        assert fileName != null;
        String title = fileName.substring(0, fileName.lastIndexOf("."));
        UploadStreamRequest request = new UploadStreamRequest(
                ConstantPropertiesUtils.KEY_ID,
                ConstantPropertiesUtils.KEY_SECRET,
                title,  // 阿里云显示的名称
                fileName, // 原始名称
                file.getInputStream()
        );
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);

        String videoId;
        if (response.isSuccess()) {
            videoId = response.getVideoId();
        } else {
            throw new GuliException(20001, response.getMessage());
        }
        return videoId;
    }

    @Override
    public void deleteAlyVideo(String videoId) throws Exception {
        DefaultAcsClient client = AlyVodClient.initVodClient();
        DeleteVideoRequest request = new DeleteVideoRequest();
        request.setVideoIds(videoId);
        client.getAcsResponse(request);
    }

    @Override
    public void removeMoreAlyVideo(List<String> videoIdList) throws ClientException {
        DefaultAcsClient client = AlyVodClient.initVodClient();
        DeleteVideoRequest request = new DeleteVideoRequest();
        String join = StringUtils.join(videoIdList.toArray(), ",");
        request.setVideoIds(join);
        client.getAcsResponse(request);
    }

    @Override
    public String getPlayAuth(String vid) throws ClientException {
        DefaultAcsClient client = AlyVodClient.initVodClient();
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(vid);
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);
        return response.getPlayAuth();
    }
}
